package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.Provider;
import com.neusoft.ecommerce.entity.Storage;
import com.neusoft.ecommerce.service.ProviderService;

/**
 * 
 * @author YinKuenTam
 *
 */
@RestController
public class ProviderController {
	private final ProviderService providerService;
	
	public ProviderController(ProviderService providerService) {
		this.providerService = providerService;
	}
	@GetMapping("/provider_page")
    public ModelAndView providerPage() {
        return new ModelAndView("provider/provider-list");
    }
	
	 @RequestMapping("/provider/list")
	    @ResponseBody
	    public LayuiPager<Provider> list(@RequestParam(value = "page", defaultValue = "1") String page,
	                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
	        System.out.println(page + "" + limit);
	        LayuiPager<Provider> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
	        int count = providerService.findTotal(pager);
	        List<Provider> list = providerService.findList(pager);
	        for (Provider provider : list) {
	            System.out.println(provider);
	        }
	        pager.setData(list);
	        pager.setCount(count);
	        pager.setCode(0);
	        pager.setMsg("");
	        return pager;
	    }
	 @GetMapping("/addProvider")
	    public ModelAndView addProvider() {
	        return new ModelAndView("provider/provider-add");
	    }
	 @PostMapping("/provider/add")
	 public Map<String, Object> add(Provider provider) {
	        System.out.println(provider);
	        Map<String, Object> map = new HashMap<>(16);
	        if (providerService.add(provider)) {
	            map.put("msg","ok");
	            map.put("code",0);
	        } else {
	            map.put("msg","失败");
	            map.put("code",1);
	        }
	        return map;
	    }
	 @GetMapping("/provider/edit_page")
	    public ModelAndView editPage(Provider provider, Model model) {
	        System.out.println(provider);
	        LayuiPager<Provider> pager = new LayuiPager<>(1,1);
	        pager.setCondition(provider);
	        Provider message = providerService.findList(pager).get(0);
	        System.out.println(message);
	        ModelAndView view = new ModelAndView("provider/provider-edit");
	        model.addAttribute("provider", message);
	        return view;
	    }
	 
	 @PostMapping("/provider/edit")
	 public Map<String, Object> edit(Provider provider) {
	        System.out.println(provider);
	        Map<String, Object> map = new HashMap<>(16);
	        if (providerService.edit(provider)) {
	            map.put("msg","ok");
	            map.put("code",0);
	        } else {
	            map.put("msg","失败");
	            map.put("code",1);
	        }
	        return map;
	    }
	 @PostMapping("/provider/delete")
	 public Map<String, Object> delete(Provider provider) {
	        System.out.println(provider);
	        Map<String, Object> map = new HashMap<>(16);
	        if (providerService.delete(provider)) {
	            map.put("msg","ok");
	            map.put("code",0);
	        } else {
	            map.put("msg","失败");
	            map.put("code",1);
	        }
	        return map;
	    }
	 
	 


}
